package com.itheima.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.ClazzService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClazzService clazzService;

    /**
     * 条件分页查询
     *
     * @param student
     */
    @Override
    public PageResult queryPage(StudentQueryParam student) {
        //1.设置分页、排序参数
        Page<Student> page = Page.of(student.getPage(), student.getPageSize());
        page.addOrder(OrderItem.desc("update_time"));

        //2.构造查询条件
        //3.发起分页查询
        //2+3(主流)
        Page<Student> studentPage = this.lambdaQuery().like(StrUtil.isNotBlank(student.getName()), Student::getName, student.getName())
                .eq(ObjectUtil.isNotNull(student.getDegree()), Student::getDegree, student.getDegree())
                .eq(ObjectUtil.isNotNull(student.getClazzId()), Student::getClazzId, student.getClazzId())
                .page(page);

        //4.对结果集进行处理
        List<Student> records = studentPage.getRecords();
        records.forEach(stu -> {
            Clazz clazzById = clazzService.getClazzById(stu.getClazzId());
            if (ObjectUtil.isNotNull(clazzById)) {
                stu.setClazzName(clazzById.getName());
            }
        });
        //5.返回分页结果集
        return new PageResult<>(studentPage.getTotal(), records);
    }

    /**
     * 新增学生信息
     *
     * @param student
     */
    @Override
    public void insertStudent(Student student) {
        //1.设置创建时间
        student.setCreateTime(LocalDateTime.now());
        //2.设置修改时间
        student.setUpdateTime(LocalDateTime.now());
        //2.新增学生信息
        this.studentMapper.insert(student);
        //3.设置班级名称
        Clazz clazz = this.clazzService.getClazzById(student.getClazzId());
        if (ObjectUtil.isNotNull(clazz)) {
            student.setClazzName(clazz.getName());
        }
    }

    /**
     * 根据id来查询学生信息
     *
     * @param id
     * @return
     */
    @Override
    public Student getStudentById(Integer id) {
        //1.查询学生信息
        Student student = this.studentMapper.selectById(id);
        //2.查询班级信息
        Clazz clazz = this.clazzService.getById(student.getClazzId());
        if (ObjectUtil.isNotNull(clazz)) {
            student.setClazzName(clazz.getName());
        }
        return student;
    }

    /**
     * 修改学生信息
     *
     * @param student
     */
    @Override
    public void updateStudent(Student student) {
        //1.设置修改时间
        student.setUpdateTime(LocalDateTime.now());
        //2.修改学生信息
        this.studentMapper.updateById(student);
        //3.修改班级名称
        Clazz clazz = this.clazzService.getById(student.getClazzId());
        if (ObjectUtil.isNotNull(clazz)) {
            student.setClazzName(clazz.getName());
        }
    }

    /**
     * 根据id删除学生信息
     *
     * @param ids
     */
    @Override
    public void deleteStudent(List<Integer> ids) {
        //1.删除学生信息
        this.studentMapper.deleteByIds(ids);
    }

    /**
     * 扣分
     *
     * @param id
     * @param score
     */
    @Override
    public void violation(Integer id, Integer score) {
        //1.根据id查询学生信息
        Student student = this.studentMapper.selectById(id);

        //2.次数+1 扣分
        student.setViolationCount((Short.valueOf(String.valueOf(student.getViolationCount() + 1))));
        student.setViolationScore((Short.valueOf(String.valueOf(student.getViolationScore() + score))));
        //3.更新数据
        this.studentMapper.updateById(student);

    }
}
